import { chooseImage, previewImage, showToast, uploadFile } from '../../utils/util';

Page({
  data: {
    tabs: [
      {
        id: 0,
        title: '体验问题',
        isActive: true
      },
      {
        id: 1,
        title: '商品、商家问题',
        isActive: false
      }
    ],
    imgList: [],
    textVal: 'test'
  },
  onLoad() {
    let imgList = wx.getStorageSync('img') || [];
    this.setData({
      imgList
    })
  },
  handleTabClick(e) {
    let id = e.detail === undefined ? e : e.detail.id;
    let { tabs } = this.data;
    tabs.forEach(v => {
      if (v.id == id) {
        return v.isActive = true;
      }
      return v.isActive = false;
    });
    this.setData({ tabs })
  },
  // 上传本地图片
  handleUpImg: async function () {
    try {
      let res = await chooseImage();
      let { imgList } = this.data;
      imgList.push(...res.tempFilePaths);

      this.setData({
        imgList
      })
      wx.setStorageSync('img', imgList);
    } catch (error) {
      console.log(error);
    }
  },
  // 点击放大预览图片
  handleImgClick: async function(e) {
    let {imgSrc} = e.detail;
    await previewImage({
      urls: this.data.imgList,
      current: imgSrc
    })
  },
  // 删除图片
  handleImgDel(e) {
    let {index} = e.currentTarget.dataset;
    let {imgList} = this.data;
    imgList.splice(index, 1);
    this.setData({imgList})
  },
  // 获取输入信息
  handleTextVal(e) {
    this.setData({
      textVal: e.detail.value
    })
  },
  // 表单上传
  handleFormSubmit: async function() {
    let {textVal, imgList} = this.data;
    if(!textVal.trim()) {
      await showToast('输入不合法');
      return;
    }
    wx.showLoading({
      title: '上传中...',
      mask: true
    });
    setTimeout(async () => {
      wx.hideLoading();
      await showToast('上传成功');
      this.setData({
        imgList: [],
        textVal: ''
      })
    }, 1000)      

    // imgList.forEach(async (v, i) => {
    //   wx.uploadFile({
    //     url: 'https://imgurl.org/',
    //     filePath: v,
    //     name: 'file',
    //     success: res => {
    //       // console.log(JSON.parse(res.data));
    //       console.log(res);
    //     },
    //     fail: () => {}
    //   });
    // })

  }
})